﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUser.aspx.cs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script  runat="server">
    void Page_Load(object sender, EventArgs e){
    }
        
    void Page_Unload(object sender, EventArgs e){
    }
    
    void submit(object sender, EventArgs e){
        lblErr.ForeColor = System.Drawing.Color.Red;
        //check name field
        if (tbName.Text.Length == 0){
            lblErr.Text = "Please provide a user name";
            return;
        }
        if (tbName.Text.Length > 50){
            lblErr.Text = "User name must be less than 51 characters";
            return;
        }
        //Check password fields
        if (tbPW.Text != tbPW2.Text){
            lblErr.Text = "Passwords do not match!";
            return;
        }
        if (tbPW.Text.Length < 6){
            lblErr.Text = "Password length must be at least 6 characters";
            return;
        }
        if (tbPW.Text.Length > 50){
            lblErr.Text = "Password length must be less than 51 characters";
            return;
        }
        //open database connection
        SaaS.MiniSaaSDB db = new SaaS.MiniSaaSDB();
        db.open();
        if (!db.isOpen()){
            lblErr.Text = "Could not connect to database";
            return;
        }
        int rows;
        System.Data.SqlClient.SqlDataReader reader;
        //check for ID already in DB
        reader = db.executeQuery("SELECT * FROM LOGIN WHERE Login_Name = '" + tbName.Text + "';");
        if (reader.HasRows){
            lblErr.ForeColor = System.Drawing.Color.Red;
            lblErr.Text = "Error - User already exists";
            reader.Close();
            db.close();
            return;
        }
        reader.Close();
        //generate ID
        int newID;
        reader = db.executeQuery("SELECT MAX(Login_ID) + 1 FROM LOGIN");
        if (reader.HasRows){
            reader.Read();
            newID = (int)reader[0];
        }
        else{
            newID = 1;
        }
        reader.Close();
        //Insert values
        String strCommand = "INSERT INTO LOGIN (Login_ID, Login_Name, Login_Password) VALUES (" + newID.ToString() + ", '" + tbName.Text + "', '" + tbPW.Text + "');";
        rows = db.executeNonQuery(strCommand);
        if (rows <= 0){
            lblErr.Text = "Error - Could not add new user";
        }
        else{
            lblErr.ForeColor = System.Drawing.Color.Green;
            lblErr.Text = "SUCCESS!";
        }
        db.close();
        return;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<body background="https://minisaas.blob.core.windows.net/img/bg.jpg">
    <form id="frmAddUser" runat="server">
        <a href = UserLogin.aspx>Return to Login page</a><br />
        <br />
        Enter Tenant Name:
        <asp:TextBox id="tbName" runat="server" />
        <br /><br />
        Password:
        <asp:TextBox id="tbPW" TextMode="password" runat="server" />
        <br /><br />
        Re-Enter Password:
        <asp:TextBox id="tbPW2" TextMode="password" runat="server" />
        <br /><br />
        <asp:Button id="button1" Text="Submit" runat="server" OnClick="submit" />
        <br /><br />
        <asp:Label id="lblErr" runat="server"></asp:Label> 
        <br /><br />
        <asp:Label id="lblTest" runat="server"></asp:Label> 
    </form>
</body>
</html>

